package xhe.api.log.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import net.sf.json.JSONObject;
import xhe.api.log.service.GatewayService;
import xhe.entities.ApiEntity;
import xhe.entities.LogEntity;
import xhe.enums.ExecuteEnum;

@RestController
//@ComponentScan(basePackages = "xhe.api.service")
//@MapperScan(basePackages = "xhe.api.mapper")
public class LogAPI {

	@Autowired
	public GatewayService iGateway;
	@Autowired
	private DiscoveryClient iClient;

	@RequestMapping(value = "log/openapi")
	public void openAPI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		System.out.println("doPost");
		// 添加请求头,解决跨域问题
		response.addHeader("Access-Control-Allow-Origin", "*");
		// 设置响应内容类型
		response.setContentType("application/json; charset=utf-8");

		// 声明请求
		ApiEntity apiEntity = new ApiEntity();

		try {

			apiEntity.setRequest(xhe.lib.data.Util.getRequest(request.getInputStream()));

			// 解析请求串(json)
			JSONObject jsonObject = JSONObject.fromObject(apiEntity.getRequest());

			// 获取方法名称
			apiEntity.setMethodName(jsonObject.getString("Method"));
			// 获取请求参数
			apiEntity.setParams(jsonObject.getString("Params"));
			// 获取Token
			apiEntity.setToken(jsonObject.getString("Token"));
			// 获取数据链接类型
			apiEntity.setDBType(jsonObject.getString("DBType"));

			System.out.println("Request:" + apiEntity.getRequest());
			// System.out.println("Method:" + apiEntity.getMethod());
			// System.out.println("Params:" + apiEntity.getParams());
			// System.out.println("Token:" + apiEntity.getToken());
			// System.out.println("DBType:" + apiEntity.getDBType());

			// 验证登陆信息
			if (StringUtils.isEmpty(apiEntity.getToken())) {

				apiEntity.setCode(ExecuteEnum.AuthError);
				apiEntity.setMessage("登陆会话已过期");
			} else if (StringUtils.isEmpty(apiEntity.getDBType())) {

				apiEntity.setCode(ExecuteEnum.DBError);
				apiEntity.setMessage("初始化数据连接出错");
			} else {

				LogEntity property = new LogEntity();
				property.setLoginName("adminhx");
				property.setUserName("何旭");
				property.setAuthCode("askdajd-asdlasdj-iewrnn");
				property.setBareausCode("S01");
				property.setBussinessCode("RS01");
				property.setServiceCode("GRQY001");
				property.setRequestParams("{}");
				property.setDataCode("--");
				property.setDataType("--");
				property.setDataTotal("--");
				property.setRequestTime("2020-10-14 00:00:00");
				property.setResponseTime("2020-10-14 10:00:00");
				property.setExecuteTime(20);
				property.setResponseText("");
				property.setStatus(ExecuteEnum.AuthError);
				property.setMessage("");
				property.setRemarks("");
				iGateway.writeLog(property);

			}

		} catch (Exception ex) {
			apiEntity.setCode(ExecuteEnum.Error);
			apiEntity.setMessage(ex.getMessage());
			ex.printStackTrace();

		}

		System.out.println(apiEntity.getResponse());

		// 输出返回结果
		PrintWriter out = response.getWriter();
		out.println(apiEntity.getResponse());
	}

	@RequestMapping(value = "log/discovery", method = RequestMethod.GET)
	public Object discovery() {
		List<String> list = iClient.getServices();
		System.out.println("**********" + list);

		LogEntity property = new LogEntity();
		property.setLoginName("adminhx");
		property.setUserName("何旭");
		property.setAuthCode("askdajd-asdlasdj-iewrnn");
		property.setBareausCode("S01");
		property.setBussinessCode("RS01");
		property.setServiceCode("GRQY001");
		property.setRequestParams("{}");
		property.setDataCode("--");
		property.setDataType("--");
		property.setDataTotal("--");
		property.setRequestTime("2020-10-14 00:00:00");
		property.setResponseTime("2020-10-14 10:00:00");
		property.setExecuteTime(20);
		property.setResponseText("");
		property.setStatus(ExecuteEnum.AuthError);
		property.setMessage("");
		property.setRemarks("");
		iGateway.writeLog(property);

		List<ServiceInstance> srvList = iClient.getInstances("LogOpenAPI");
		for (ServiceInstance element : srvList) {
			System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
					+ element.getUri());
		}
		return this.iClient;
	}

//	public LogEntity property;
//
//	public OpenAPI() {
//		property = new LogEntity();
//
//	}

	/**
	 * <b> 方法: </b> 记录网关日志
	 * 
	 * @author xhe
	 * @param LogEntity 日志属性类
	 * @return Boolean 成功/失败
	 * @version V1.0.0.0
	 * @version --2020年10月14日
	 * @version --新增方法
	 * @throws Exception
	 */
	@RequestMapping(value = "log/write", method = RequestMethod.GET)
	public Boolean write() {

		Boolean blResult = false;
		try {

			LogEntity property = new LogEntity();

			property.setLoginName("adminhx");
			property.setUserName("何旭");
			property.setAuthCode("askdajd-asdlasdj-iewrnn");
			property.setBareausCode("S01");
			property.setBussinessCode("RS01");
			property.setServiceCode("GRQY001");
			property.setRequestParams("{}");
			property.setDataCode("--");
			property.setDataType("--");
			property.setDataTotal("--");
			property.setRequestTime("2020-10-14 00:00:00");
			property.setResponseTime("2020-10-14 10:00:00");
			property.setExecuteTime(20);
			property.setResponseText("");
			property.setStatus(ExecuteEnum.AuthError);
			property.setMessage("");
			property.setRemarks("");

			System.out.println("LoginName:" + property.getLoginName());
			System.out.println("UserName:" + property.getUserName());
			System.out.println("AuthCode:" + property.getAuthCode());
			System.out.println("BareausCode:" + property.getBareausCode());
			System.out.println("BussinessCode:" + property.getBussinessCode());
			System.out.println("ServiceCode:" + property.getServiceCode());
			System.out.println("RequestParams:" + property.getRequestParams());
			System.out.println("DataName:" + property.getDataCode());
			System.out.println("DataType:" + property.getDataType());
			System.out.println("DataTotal:" + property.getDataTotal());
			System.out.println("RequestTime:" + property.getRequestTime());
			System.out.println("ResponseTime:" + property.getResponseTime());
			System.out.println("ExecuteTime:" + property.getExecuteTime());
			System.out.println("ResponseText:" + property.getResponseText());
			System.out.println("Status:" + property.getStatus());
			System.out.println("Message:" + property.getMessage());
			System.out.println("Remarks:" + property.getRemarks());

			return iGateway.writeLog(property);

		} catch (Exception ex) {
			System.out.println(ex.getMessage());
			ex.printStackTrace();
		}

		return blResult;
	}
}